package com.salinity.kun.task;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.salinity.kun.algorithm.harmonic.GaussianElimination;
import com.salinity.kun.algorithm.harmonic.TideSymbol;
import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.helper.CalculationHelper;
import com.salinity.kun.helper.ConstantHelper;
import com.salinity.kun.helper.HarmonicHelper;
import com.salinity.kun.helper.OracleHelper;
import com.salinity.kun.model.FourierSeriesInput;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;

public class AmplitudeSolutionTask implements ITask {

	@Override
	public boolean run() throws IOException, ParseException {

		String dateStr = "2018/06/1 0:0:0";
		String stationID = "2c9240214f50ba3a0150655a06be000c";
		SimpleDateFormat sdf = ConstantHelper.getDateFormat();
		int[] intervals = { 1, 5 };
		Path outputPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\test\\");
		int hours = 0;
		int rawDeltaT = 5;
		Date startDate = sdf.parse(dateStr), endDate;
		List<StationDateData> zjTideList = null;

		// 72h
		List<TideSymbol> tideSymbolList = new ArrayList<>();
		tideSymbolList.add(TideSymbol.M2);
		tideSymbolList.add(TideSymbol.S2);
		tideSymbolList.add(TideSymbol.N2);
		tideSymbolList.add(TideSymbol.K1);
		tideSymbolList.add(TideSymbol.O1);
		tideSymbolList.add(TideSymbol.Q1);
		tideSymbolList.add(TideSymbol.M4);
		tideSymbolList.add(TideSymbol.MS4);
		tideSymbolList.add(TideSymbol.M6);
		hours = 72;

		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours));
		zjTideList = BaseHydrologyDataHelper.getTidesFromDB(stationID, startDate, endDate);
		zjTideList = BaseHydrologyDataHelper.doLinearInterpolation(zjTideList, startDate, endDate, 5);

		cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, zjTideList,
				outputPath.resolve("ZhenJiang_" + hours));
		
		//96
		hours = 96;

		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours));
		zjTideList = BaseHydrologyDataHelper.getTidesFromDB(stationID, startDate, endDate);
		zjTideList = BaseHydrologyDataHelper.doLinearInterpolation(zjTideList, startDate, endDate, 5);

		cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, zjTideList,
				outputPath.resolve("ZhenJiang_" + hours));
		
		//120
		
		hours = 120;

		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours));
		zjTideList = BaseHydrologyDataHelper.getTidesFromDB(stationID, startDate, endDate);
		zjTideList = BaseHydrologyDataHelper.doLinearInterpolation(zjTideList, startDate, endDate, 5);

		cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, zjTideList,
				outputPath.resolve("ZhenJiang_" + hours));
//		// 48
//		tideSymbolList.remove(TideSymbol.Q1);
//		hours = 48;
//
//		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours));
//		zjTideList = BaseHydrologyDataHelper.getTidesFromDB(stationID, startDate, endDate);
//		zjTideList = BaseHydrologyDataHelper.doLinearInterpolation(zjTideList, startDate, endDate, 5);
//		cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, zjTideList,
//				outputPath.resolve("ZhenJiang_" + hours));
//
//		// 36
//		tideSymbolList.remove(TideSymbol.N2);
//		tideSymbolList.remove(TideSymbol.O1);
//		hours = 36;
//
//		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours));
//		zjTideList = BaseHydrologyDataHelper.getTidesFromDB(stationID, startDate, endDate);
//		zjTideList = BaseHydrologyDataHelper.doLinearInterpolation(zjTideList, startDate, endDate, 5);
//		cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, zjTideList,
//				outputPath.resolve("ZhenJiang_" + hours));
//
//		// 24
//		tideSymbolList.remove(TideSymbol.S2);
//		tideSymbolList.remove(TideSymbol.MS4);
//		hours = 24;
//
//		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours));
//		zjTideList = BaseHydrologyDataHelper.getTidesFromDB(stationID, startDate, endDate);
//		zjTideList = BaseHydrologyDataHelper.doLinearInterpolation(zjTideList, startDate, endDate, 5);
//		cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, zjTideList,
//				outputPath.resolve("ZhenJiang_" + hours));
		System.out.println("complete...");

		return true;
	}

	public void cal(Date startDate, Date endDate, int rawDeltaT, int[] newDeltaTArray, int hours,
			List<TideSymbol> tideSymbolList, List<StationDateData> dataList, Path outputPath)
			throws ParseException, IOException {
		
		// 傅里叶分解
		FourierSeriesInput fsi = BaseHydrologyDataHelper.hydToFsi(dataList, rawDeltaT);

		String[] itemArray = ConstantHelper.getOutputItems();

		// 傅里叶分解检验
		CalculationHelper.writeToFile(fsi, itemArray, outputPath.resolve("_" + hours + "_" + rawDeltaT + "_fsi.csv"));
		
		HarmonicAnalysisTask HATask = new HarmonicAnalysisTask();
		for (int newDeltaT : newDeltaTArray) {

			HATask.doCalculation(startDate, endDate, rawDeltaT, newDeltaT, hours, tideSymbolList, dataList, outputPath);
		}

		HATask.saveParamsInOneFile(outputPath.resolve("AllCompare_" + hours + ".csv"));

	}

	public static void main(String[] args) {
		AmplitudeSolutionTask amplitudeSolutionTask = new AmplitudeSolutionTask();
		try {
			amplitudeSolutionTask.run();
		} catch (IOException | ParseException e) {
			e.printStackTrace();
		}
	}

}
